package demo6;

import java.lang.reflect.Array;

/**
 * @program: java-generic
 * @description:
 * @author: bjc
 * @create: 2021-08-15 16:24
 */
public class Fruit<T> {

    private T[] array;

    public Fruit(Class<T> clz,int length) {
        //通过Array.newInstance创建泛型数组
        this.array = (T[])Array.newInstance(clz,length);
    }

    /**
     * 填充数组
     * @param index
     * @param item
     */
    public void put(int index,T item){
        this.array[index] = item;
    }

    /**
     * 获取数组元素
     * @param index
     * @return
     */
    public T get(int index){
        return this.array[index];
    }

    public T[] getArray(){
        return this.array;
    }
}
